﻿using System;
using System.Collections.Generic;
using System.Text;
using Clipping.Infrastructure.Interfaces;
using Clipping.Infrastructure.Models;
using System.Web;

namespace Clipping.SearchUrlConstructors
{
    public abstract class QihooUrlConstructorBase : ISearchUrlConstruct
    {
        protected Encoding _urlEncoding = Encoding.GetEncoding("GB2312");

        protected Uri constructUrl(Infrastructure.Models.SearchObject searchObj,QihooSearchType searchType)
        {

            string searchRangePara=null;
            if (searchObj.SearchType == SearchType.SearchInTitle)
                searchRangePara = "title";
            else
                searchRangePara = "content";
            string url = string.Format("http://qihoo.com/wenda.php?r=search/index&kw={0}&do=search&noq=q&type={1}&sort=pdate&pos={2}", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding), searchType, searchRangePara);
            DateTime now=DateTime.Now;
            if (searchObj is TimespanSearchObject)
            {
                TimespanSearchObject so = searchObj as TimespanSearchObject;
                TimeSpan ts = now - so.StartTime;
                if (ts.TotalDays <= 1)
                {
                    url += "&time=day";
                }
                else if (ts.TotalDays <= 7)
                {
                    url += "&time=week";
                }
                else if (ts.TotalDays <= 30)
                {
                    url += "&time=month";
                }

            }
            else if (searchObj is PeriodSearchObject)
            {
                PeriodSearchObject so = searchObj as PeriodSearchObject;
                switch (so.SearchPeriod)
                { 
                    case SearchPeriod.最近1天:
                        url += "&time=day";
                        break;
                    case SearchPeriod.最近2天 :
                    case SearchPeriod.最近3天 :
                    case SearchPeriod.最近1周 :
                        url += "&time=week";
                        break;
                    case SearchPeriod.最近1月 :
                        url += "&time=month";
                        break;
                
                }
            }
            return new Uri(url);
        }

        public abstract Uri ConstructUrl(Infrastructure.Models.SearchObject searchObj);
        

        public Uri ConstructAbsoluteUrl(Uri baseUri, string url)
        {
            return UriHelper.ConstructAbsoluteUrl(baseUri, url);
        }

        public enum QihooSearchType
        {
            wenda,
            news,
            bbs,
            blog
        }
    }
    public class QihooNewsUrlConstructor : ISearchUrlConstruct
    {

           protected Encoding _urlEncoding = Encoding.GetEncoding("GB2312");

        public  Uri ConstructUrl(Infrastructure.Models.SearchObject searchObj)
        {
            string tn=null;
            if(searchObj.SearchType == SearchType.SearchInContent )
                tn="news";
            else
                tn="newstitle";
            string url = string.Format("http://news.so.com/ns?rank=pdate&src=srp&q={0}&tn={1}", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding),tn );
         return new Uri(url);
        
        }


        public Uri ConstructAbsoluteUrl(Uri baseUri, string url)
        {
            return UriHelper.ConstructAbsoluteUrl(baseUri, url);
        }
    }
    public class QihooBlogUrlConstructor : QihooUrlConstructorBase
    {
        public override Uri ConstructUrl(Infrastructure.Models.SearchObject searchObj)
        {
            return constructUrl(searchObj, QihooSearchType.blog);
        }
    }
    public class QihooBBSUrlConstructor : QihooUrlConstructorBase
    {
        public override Uri ConstructUrl(Infrastructure.Models.SearchObject searchObj)
        {
            return constructUrl(searchObj, QihooSearchType.bbs);
        }
    }
    public class QihooWendaUrlConstructor : QihooUrlConstructorBase
    {
        public override Uri ConstructUrl(Infrastructure.Models.SearchObject searchObj)
        {
            return constructUrl(searchObj, QihooSearchType.wenda);
        }
    }
}
